const app = getApp()
const md = require('../../utils/md5.js');
const post = require('../../utils/post.js');
Page({
  data: {
    mainactive:0,
  },
  // 初始化
  onLoad: function (options) {
    
  },

  onShow:function(){
    var that = this;
    var phone = (wx.getStorageSync('phone'));
    if(!phone){
      phone='null';
    }
    var apinameIsMeb = "/BO/BA_Member/Query";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        where: 'Tel =' + phone
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if (res.data.data.length != 0) { 
          var state='';
          if (res.data.data[0].ApplyBOState){
            state = res.data.data[0].ApplyBOState
          }else{
            state=0;
          }
          var star = res.data.data[0].StarLevel;
          that.setData({
            userinfo: res.data.data[0],
            // state: state,
            // star: star,
            // starnone: 5 - star 
          })
          wx.setStorageSync('userinfo', res.data.data[0]);
          that.getapply(res.data.data[0].MebID);
        } else {
          wx.navigateTo({
            url: '/pages/regist/regist',
          })
        }
      }
    })
    that.setData({
      newinfo: wx.getStorageSync('newinfo')
    })
  },
  getapply2: function (mebid) {
    var that = this;
    // var depid = wx.getStorageSync('depid');
    if (mebid) {
      var apinameIsMeb = "/BO/AP_ShopApply/Query";
      var timestamp = new Date().getTime();
      timestamp = parseInt(timestamp / 1000);
      var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
      wx.request({
        //后台接口地址
        url: app.server.url + apinameIsMeb,
        data: {
          sign: md5IsMeb,
          appid: app.server.appid,
          tmsp: timestamp,
          where: "MebID =" + mebid
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
          if (res.data.data.length > 0) {
            wx.setStorageSync('apply', res.data.data[0]);
            // var star = res.data.data[0].StarLevel ? res.data.data[0].StarLevel : "";
            var star =1;
            that.setData({
              apply: res.data.data[0],
              star: 1,
              starnone: 5 - star
            })
          }
        }
      })
    }
  },
  getapply: function (mebid) {
    var that = this;
    var depid = wx.getStorageSync('depid') ? wx.getStorageSync('depid'):0;
    console.log("AccID =" + depid + " and MebID = " + mebid)
    if (mebid) {
      var apinameIsMeb = "/BO/V_MebApplyStatus/Query";
      var timestamp = new Date().getTime();
      timestamp = parseInt(timestamp / 1000);
      var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
      wx.request({
        //后台接口地址
        url: app.server.url + apinameIsMeb,
        data: {
          sign: md5IsMeb,
          appid: app.server.appid,
          tmsp: timestamp,
          where:"AccID ="+depid +" and MebID = "+mebid
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
          if (res.data.data.length > 0) {
            wx.setStorageSync('apply', res.data.data[0]);
            var star = res.data.data[0].StarLevel ? res.data.data[0].StarLevel : "";
            that.setData({
              apply: res.data.data[0],
              star: star,
              starnone: 5 - star
            })
          }
        }
      })
    }
  },
  onReady: function () {
    // this.setData({
    //   userinfo: wx.getStorageSync('userinfo')
    // })
  },
  changeqingdan:function(e){
    console.log(this.data.mainactive);
    this.setData({
      mainactive:1
    })
  },
  changecaigou: function(e) {
    this.setData({
      mainactive:0
    })
  },
  tosafe:function(){
    wx.navigateTo({
      url: '/pages/safe/safe',
    })
  },
  editperson:function(){
    wx.navigateTo({
      url: '/pages/editperson/editperson',
    })
  },
  toapply: function () {
    var info = this.data.apply;
    var mebid = this.data.userinfo.MebID;
    if(!mebid || mebid=='undifine'){
      wx.showModal({
        title: '',
        content: '个人信息还未加载完，请稍后再试...',
      })
      return;
    }
    if(info){
      wx.navigateTo({
        url: '/pages/applystore/applystore?applystate=' + info._BOSName + '&applycode=' + info.ApplyCode,
      })
    }else{
      wx.navigateTo({
        url: '/pages/applystore/applystore',
      })
    }
    
  },
  toauthsafe:function(){
    wx.navigateTo({
      url: '/pages/certification/certification',
    })
  },
  tomyqrcode(){
    wx.navigateTo({
      url: '/pages/myqrcode/myqrcode',
    })
  },
  toservice:function(){
    var info = wx.getStorageSync('userinfo').ServiceMebID;
    if(!info){
      info='';
    }
    wx.navigateTo({
      url: '/pages/service/service?id=' + info
    })
    
  },
  saoyisao: function () {
    var that =this;
    wx.scanCode({
      success: (res) => {
        console.log(res.result);
       var mebarr =res.result.split(':');
       if(mebarr[0]=='mebid' &&　mebarr[1] !=''){
        console.log(111);
        var datas = {};
        datas.where = "MebID = "+mebarr[1];
        var datacall = function (res) {
          console.log(res);
          if (res.data.data.length != 0) {
            if (!res.data.data[0].IsService){
              wx.showModal({
                title: '',
                content: '对方不是客户经理，无法添加',
              })
              return;
            }else{
              console.log("oktian");
              that.addservice(res.data.data[0].MebID,res.data.data[0].MebName,res.data.data[0].Tel);
            }
          }
        }
        var res2 = post.getdata("/BO/BA_Member/Query", datas, datacall);

       }else{
         wx.showModal({
           title: '',
           content: '不是符合规定的二维码',
         })
       }   
      }
    })
  },
  //添加客户经理
  addservice:function(id,name,phone){
    var that =this;
    var datas = {};
    var data ={};
    data.MebID =that.data.userinfo.MebID;
    data.ServiceMebID = id;
    data.ServiceTel = phone;
    data.ServiceMebName = name;
    datas.data = JSON.stringify(data);
    var datacall = function (res) {
      console.log(res);
      wx.showModal({
        title: '',
        content: '添加客户经理成功',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/service/service?id=' + id
            })
          } else if (res.cancel) {
            return;
          }
        }
      })
      
    }
    var res2 = post.getdata("/BO/BA_Member/Modify", datas, datacall);
  },
  loginout:function(){
    var that = this;
    var datas = {};
    datas.MebID = that.data.userinfo.MebID;
    var datacall = function (res) {
      console.log(res);
      if(res.data.data){
        wx.clearStorage();
        wx.redirectTo({
          url: '/pages/login/login',
        })
      }
    }
    var res2 = post.getdata("/Action/PK01/MemberLoginOut", datas, datacall);
  },
  tocenter:function(){
    wx.navigateTo({
      url: '/pages/center/center',
    })
  },
  makesurecode:function(){
    var that = this;
    wx.showModal({
      title: '',
      content: '是否已经领取到收款二维码？',
      success: function (res) {
        if (res.confirm) {
          var datas = {};
          datas.AccID = wx.getStorageSync('depid');
          var datacall = function (res) {
            console.log(res);
            var applist = that.data.apply;
            applist.QRStatus=0;
            that.setData({
              apply:applist
            })
            wx.showModal({
              title: '',
              content: '确认成功！',
            })
            return;
          }
          var res2 = post.getdata("/Action/PK01/QRSendOver", datas, datacall);
        } else if (res.cancel) {
          return;
        }
      }
    })
  }

})
